package com.device.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.device.Constant.BaseConstant;
import com.device.Entity.Device;
import com.device.Entity.Dto.SessionWebUserDto;
import com.device.Entity.Lab;
import com.device.Exception.BaseException;
import com.device.Mapper.DeviceMapper;
import com.device.Service.DeviceService;
import com.device.annotation.GlobalInterceptor;
import com.device.common.PageResult;
import com.device.common.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpHeaders;

@RequestMapping("/device")
@RestController
public class DeviceController {
    @Resource
    private DeviceService deviceService;

    @RequestMapping("/add")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result add(Device device) {
        deviceService.adddevice(device);

        return Result.success();
    }
    @RequestMapping("/edit")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result edit(Device device) {
        deviceService.editdevice(device);

        return Result.success();
    }
    @RequestMapping("/list")
    @GlobalInterceptor(checkParam = true, checkLogin = true)
    public Result list(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = deviceService.getpage(pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }
    @RequestMapping("/ulist")
    @GlobalInterceptor(checkParam = true, checkLogin = true)
    public Result ulist(HttpSession httpSession, Integer pageNo, Integer pageSize, String keyword) {
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        Integer uid=sessionWebUserDto.getId();
        PageResult pageResult = deviceService.getpage1(uid,pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }

    @RequestMapping("/delete")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result delete(Integer id) {

        Device device = deviceService.getById(id);
        if (device == null) {
            throw new BaseException("该设备不存在");
        }

        deviceService.removeById(id);
        return Result.success();
    }
}
